//
//  EFMyCollectionGoodsViewCell.m
//  EnjogFood
//
//  Created by guocheng on 2017/5/8.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFMyCollectionGoodsViewCell.h"
#import "EFMyCollectionGoodsViewItem.h"
#import "iToast.h"
#import "EFSepecialItem.h"


@interface EFMyCollectionGoodsViewCell ()

@property(strong,nonatomic) UIImageView* goodsIV;
@property(strong,nonatomic) UILabel* priceLabel;
@property(strong,nonatomic) UILabel* titleLabel; 
@property(strong,nonatomic) UIButton* addBtn;

@property(strong,nonatomic)NSString* productId;

@end

@implementation EFMyCollectionGoodsViewCell



-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor = [UIColor whiteColor];
        [self initView];
    }
    return self;
}

-(void)setObject:(EFMyCollectionGoodsViewItem *)item{
    self.productId = item.id;
    [self.titleLabel setText:item.product_name];
    [self.titleLabel sizeToFit];
    if (self.titleLabel.height > 60) {
        self.titleLabel.height = 60;
    }

    NSString* strPrice = [NSString stringWithFormat:@"￥%@",item.price];
    [self.priceLabel setText:strPrice];
     [self.priceLabel sizeToFit];
    [self.goodsIV setImageWithURL:[NSURL URLWithString:item.img]];
    
}

-(void)initView{
    [self.contentView addSubview:self.goodsIV];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.priceLabel];
    [self.contentView addSubview:self.addBtn];
}

-(void)layoutSubviews{
    [super layoutSubviews];
    self.goodsIV.width = 102;
    self.goodsIV.height = 102;
    self.goodsIV.left = 20;
    self.goodsIV.top = 19;
    
    self.titleLabel.left = self.goodsIV.right + 9;
    self.titleLabel.top = 6;
    self.titleLabel.height = 60;
    self.titleLabel.width= kScreenWidth - self.goodsIV.width - 30;
    
    self.priceLabel.left = self.goodsIV.right + 9;
    self.priceLabel.top = self.titleLabel.bottom + 27;
    self.priceLabel.height = 14;
    
    self.addBtn.width = 18;
    self.addBtn.height = 18;
    self.addBtn.left = kScreenWidth - 20  - 18;
    self.addBtn.top = self.priceLabel.top;
}

-(UIImageView*)goodsIV{
    if(_goodsIV == NULL){
        _goodsIV = [[UIImageView alloc] init];
        _goodsIV.backgroundColor = RGBCOLOR(0xf3f3f3);
        _goodsIV.contentMode = UIViewContentModeScaleAspectFill;
        _goodsIV.clipsToBounds = YES;
    }
    return _goodsIV;
}

-(UILabel*)titleLabel{
    if(_titleLabel == NULL){
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:15];
        _titleLabel.textColor  = kBlack333;
        //_titleLabel.lineBreakMode  = NSLineBreakByTruncatingTail;
        _titleLabel.numberOfLines = 0 ;
    }
    return _titleLabel;
}

-(UILabel*)priceLabel{
    if(_priceLabel == NULL){
        _priceLabel = [[UILabel alloc] init];
        _priceLabel.font = [UIFont systemFontOfSize:13];
        _priceLabel.textColor  = RGBCOLOR(0x69BA45);
    }
    return _priceLabel;
} 

-(UIButton*)addBtn{
    if(_addBtn == NULL){
        _addBtn = [[UIButton alloc] init];
        [_addBtn setTitleColor:RGBCOLOR(0x69BA45) forState:UIControlStateNormal];
        [_addBtn setBackgroundImage:IMG(@"buy_icon_home_default") forState:UIControlStateNormal];
        [_addBtn addTarget:self action:@selector(onClickAdd) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addBtn;
}

-(void)onClickAdd{
    [APP_CONTEXT.net AddCartId:self.productId qty:[NSNumber numberWithInteger:1] type:[NSNumber numberWithInteger:0] OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        if(code == 0){
            if ([info objectForKey:@"list"]) {
                NSArray *array = [info objectForKey:@"list"];
                [APP_CONTEXT.shoppingCartManager.storeProductsCart removeAllObjects];
                for (NSDictionary *dic in array) {
                    EFSepecialItem *specialItem = [EFSepecialItem yy_modelWithDictionary:dic];
                    if(specialItem.qty == 0 ){
                        specialItem.qty = [NSNumber numberWithInt:1];
                    }
                    [APP_CONTEXT.shoppingCartManager addToStoreProduct:specialItem];
                }
            }
            [[iToast makeText:@"加入成功"]show];
        }else{
            [[iToast makeText:message]show];
        }
    }];
}


@end
